package com.tuzhi.test;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.utils.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SevenZExtractor {
    public static void main(String[] args) {
        String filePath = "D:\\my_data\\gitee\\tuzhi-group-3\\tz-converter-java\\cadFiles\\LEB2024-2699.7z";
        String outputDir = "D:\\convert\\unziprar";

        try (SevenZFile sevenZFile = new SevenZFile(new File(filePath))) {
            ArchiveEntry entry;
            while ((entry = sevenZFile.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }

                File outputFile = new File(outputDir, entry.getName());
                File outputDirFile = outputFile.getParentFile();
                if (!outputDirFile.exists()) {
                    outputDirFile.mkdirs();
                }

                byte[] content = new byte[(int) entry.getSize()];
                sevenZFile.read(content, 0, content.length);

                try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                    fos.write(content);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}